package hotel.modal;

import hotel.dao.RoomRentDao;
import javafx.beans.property.*;

public class RoomRent {
	private SimpleStringProperty preOrderId;	// 开房单号
	private SimpleIntegerProperty customerNo;	// 客户编号
	private SimpleStringProperty roomNo;		// 房间编号
	private SimpleStringProperty startTime;		// 入住时间
	private SimpleIntegerProperty duration;		// 入住时长
	private SimpleStringProperty leaveTime;		// 退房时间
	private SimpleDoubleProperty deposit;		// 预收金额
	private SimpleStringProperty createTime;	// 创建时间
	private SimpleBooleanProperty valid;		// 有效标记
	private SimpleStringProperty operateUser1;	// 开房操作用户
	private SimpleStringProperty operateUser2;	// 退房操作用户
	private SimpleStringProperty comment;		// 备注信息
	private SimpleStringProperty typeName;		// 房间类型
	private SimpleStringProperty name;			// 客户姓名
	private SimpleStringProperty phone;			// 联系电话
	
	private Room room;
	private Customer customer;
	
	protected SimpleBooleanProperty modified;
	
	private void init(RoomRentDao rrd) {
		if(rrd != null) {
			this.preOrderId = new SimpleStringProperty(rrd.getPreOrderId());
			this.customerNo = new SimpleIntegerProperty(rrd.getCustomerNo());
			this.roomNo = new SimpleStringProperty(rrd.getRoomNo());
			this.startTime = new SimpleStringProperty(rrd.getStartTime());
			this.duration = new SimpleIntegerProperty(rrd.getDuration());
			this.leaveTime = new SimpleStringProperty(rrd.getLeaveTime());
			this.deposit = new SimpleDoubleProperty(rrd.getDeposit());
			this.createTime = new SimpleStringProperty(rrd.getCreateTime());
			this.valid = new SimpleBooleanProperty(rrd.isValid());
			this.operateUser1 = new SimpleStringProperty(rrd.getOperateUser1());
			this.operateUser2 = new SimpleStringProperty(rrd.getOperateUser2());
			this.comment = new SimpleStringProperty(rrd.getComment());
		}else {
			this.preOrderId = new SimpleStringProperty("");
			this.customerNo = new SimpleIntegerProperty(0);
			this.roomNo = new SimpleStringProperty("");
			this.startTime = new SimpleStringProperty("");
			this.duration = new SimpleIntegerProperty(1);
			this.leaveTime = new SimpleStringProperty("");
			this.deposit = new SimpleDoubleProperty(0.0);
			this.createTime = new SimpleStringProperty("");
			this.valid = new SimpleBooleanProperty(true);
			this.operateUser1 = new SimpleStringProperty("");
			this.operateUser2 = new SimpleStringProperty("");
			this.comment = new SimpleStringProperty("");
		}
		this.typeName = new SimpleStringProperty("");
		this.name = new SimpleStringProperty("");
		this.phone = new SimpleStringProperty("");
		this.modified = new SimpleBooleanProperty(false);
		checkValid();
	}
	
	public RoomRent(RoomRentDao rrd) {
		init(rrd);
	}
	
	public RoomRent() {
		init(null);
	}
	
	public Object getDao() {
		if(leaveTime.get() == null || leaveTime.get().equals("-1")) leaveTime.set(null);
		RoomRentDao rrd = new RoomRentDao();
		rrd.setPreOrderId(preOrderId.get());
		rrd.setCustomerNo(customerNo.get());
		rrd.setRoomNo(roomNo.get());
		rrd.setStartTime(startTime.get());
		rrd.setDuration(duration.get());
		if(leaveTime.get() == null || leaveTime.get().equals("")) rrd.setLeaveTime(null);
		else rrd.setLeaveTime(leaveTime.get());
		rrd.setDeposit(deposit.get());
		rrd.setCreateTime(createTime.get());
		rrd.setValid(valid.get());
		if(operateUser1.get() == null || operateUser1.get().equals("")) rrd.setOperateUser1(null);
		rrd.setOperateUser1(operateUser1.get());
		if(operateUser2.get() == null ||operateUser2.get().equals("")) rrd.setOperateUser2(null);
		else rrd.setOperateUser2(operateUser2.get());
		if(comment.get() == null || comment.get().isEmpty()) rrd.setComment(null);
		rrd.setComment(comment.get());
		return rrd;
	}
	
	public RoomRent(RoomRent roomRent) {
		this.preOrderId = new SimpleStringProperty(roomRent.getPreOrderId());
		this.customerNo = new SimpleIntegerProperty(roomRent.getCustomerNo());
		this.roomNo = new SimpleStringProperty(roomRent.getRoomNo());
		this.startTime = new SimpleStringProperty(roomRent.getStartTime());
		this.duration = new SimpleIntegerProperty(roomRent.getDuration());
		this.leaveTime = new SimpleStringProperty(roomRent.getLeaveTime());
		this.deposit = new SimpleDoubleProperty(roomRent.getDeposit());
		this.createTime = new SimpleStringProperty(roomRent.getCreateTime());
		this.valid = new SimpleBooleanProperty(roomRent.isValid());
		this.operateUser1 = new SimpleStringProperty(roomRent.getOperateUser1());
		this.operateUser2 = new SimpleStringProperty(roomRent.getOperateUser2());
		this.comment = new SimpleStringProperty(roomRent.getComment());
		this.typeName = new SimpleStringProperty(roomRent.getTypeName());
		this.name = new SimpleStringProperty(roomRent.getName());
		this.phone = new SimpleStringProperty(roomRent.getPhone());
		this.modified = new SimpleBooleanProperty(false);
		checkValid();
	}
	
	public RoomRent(String preOrderId, int customerNo, String roomNo, String startTime, int duration, 
			String leaveTime, double deposit, String createTime, int valid, String operateUser1, 
			String operateUser2, String comment) {
		this.preOrderId = new SimpleStringProperty(preOrderId);
		this.customerNo = new SimpleIntegerProperty(customerNo);
		this.roomNo = new SimpleStringProperty(roomNo);
		this.startTime = new SimpleStringProperty(startTime);
		this.duration = new SimpleIntegerProperty(duration);
		this.leaveTime = new SimpleStringProperty(leaveTime);
		this.deposit = new SimpleDoubleProperty(deposit);
		this.createTime = new SimpleStringProperty(createTime);
		if(valid > 0) this.valid = new SimpleBooleanProperty(true);
		else this.valid = new SimpleBooleanProperty(false);
		this.operateUser1 = new SimpleStringProperty(operateUser1);
		this.operateUser2 = new SimpleStringProperty(operateUser2);
		this.comment = new SimpleStringProperty(comment);
		this.modified = new SimpleBooleanProperty(false);
		checkValid();
	}
	
	public String toString() {
		String str = String.format("PreOrderId:%s CustomerNo:%d RoomNo:%s StartTime:%s Duration:%d  leaveTime:%s "+
				"Deposit:%.2f CreateTime:%s Valid:%s OperateUser1:%s OperateUser2:%s Comment:%s", 
				preOrderId.get(), customerNo.get(), roomNo.get(), startTime.get(), duration.get(), leaveTime.get(),
				deposit.get(), createTime.get(), valid.get(), operateUser1.get(), operateUser2.get(), comment.get());
		return str;
	}
	
	public void checkValid() {
		if(!this.valid.get())
			this.setLeaveTime("-1");
	}
	
	public final SimpleStringProperty preOrderIdProperty() {
		return this.preOrderId;
	}
	
	public final String getPreOrderId() {
		return this.preOrderIdProperty().get();
	}
	
	public final void setPreOrderId(final String preOrderId) {
		this.preOrderIdProperty().set(preOrderId);
	}
	
	public final SimpleIntegerProperty customerNoProperty() {
		return this.customerNo;
	}
	
	public final int getCustomerNo() {
		return this.customerNoProperty().get();
	}
	
	public final void setCustomerNo(final int customerNo) {
		this.customerNoProperty().set(customerNo);
	}
	
	public final SimpleStringProperty roomNoProperty() {
		return this.roomNo;
	}
	
	public final String getRoomNo() {
		return this.roomNoProperty().get();
	}
	
	public final void setRoomNo(final String roomNo) {
		this.roomNoProperty().set(roomNo);
	}
	
	public final SimpleStringProperty startTimeProperty() {
		return this.startTime;
	}
	
	public final String getStartTime() {
		return this.startTimeProperty().get();
	}
	
	public final void setStartTime(final String startTime) {
		this.startTimeProperty().set(startTime);
	}
	
	public final SimpleStringProperty leaveTimeProperty() {
		return this.leaveTime;
	}
	
	public final String getLeaveTime() {
		return this.leaveTimeProperty().get();
	}
	
	public final void setLeaveTime(final String leaveTime) {
		this.leaveTimeProperty().set(leaveTime);
	}
	
	public final SimpleDoubleProperty depositProperty() {
		return this.deposit;
	}
	
	public final double getDeposit() {
		return this.depositProperty().get();
	}
	
	public final void setDeposit(final double deposit) {
		this.depositProperty().set(deposit);
	}
	
	public final SimpleStringProperty createTimeProperty() {
		return this.createTime;
	}
	
	public final String getCreateTime() {
		return this.createTimeProperty().get();
	}
	
	public final void setCreateTime(final String createTime) {
		this.createTimeProperty().set(createTime);
	}
	
	public final SimpleBooleanProperty validProperty() {
		return this.valid;
	}
	
	public final boolean isValid() {
		return this.validProperty().get();
	}
	
	public final void setValid(final boolean valid) {
		this.validProperty().set(valid);
	}
	
	public final SimpleStringProperty operateUser1Property() {
		return this.operateUser1;
	}
	
	public final String getOperateUser1() {
		return this.operateUser1Property().get();
	}
	
	public final void setOperateUser1(final String operateUser1) {
		this.operateUser1Property().set(operateUser1);
	}
	
	public final SimpleStringProperty operateUser2Property() {
		return this.operateUser2;
	}
	
	public final String getOperateUser2() {
		return this.operateUser2Property().get();
	}
	
	public final void setOperateUser2(final String operateUser2) {
		this.operateUser2Property().set(operateUser2);
	}
	
	public final SimpleStringProperty commentProperty() {
		return this.comment;
	}
	
	public final String getComment() {
		return this.commentProperty().get();
	}
	
	public final void setComment(final String comment) {
		this.commentProperty().set(comment);
	}

	public final SimpleBooleanProperty modifiedProperty() {
		return this.modified;
	}
	
	public final boolean isModified() {
		return this.modifiedProperty().get();
	}

	public final void setModified(final boolean modified) {
		this.modifiedProperty().set(modified);
	}

	public final SimpleIntegerProperty durationProperty() {
		return this.duration;
	}

	public final int getDuration() {
		return this.durationProperty().get();
	}
	
	public final void setDuration(final int duration) {
		this.durationProperty().set(duration);
	}

	public final SimpleStringProperty typeNameProperty() {
		return this.typeName;
	}
	

	public final String getTypeName() {
		return this.typeNameProperty().get();
	}
	

	public final void setTypeName(final String typeName) {
		this.typeNameProperty().set(typeName);
	}
	

	public final SimpleStringProperty nameProperty() {
		return this.name;
	}
	

	public final String getName() {
		return this.nameProperty().get();
	}
	

	public final void setName(final String name) {
		this.nameProperty().set(name);
	}
	

	public final SimpleStringProperty phoneProperty() {
		return this.phone;
	}
	

	public final String getPhone() {
		return this.phoneProperty().get();
	}
	

	public final void setPhone(final String phone) {
		this.phoneProperty().set(phone);
	}
	
	public Room getRoom() {
		return room;
	}

	public void setRoom(Room room) {
		this.room = room;
		if(room != null) {
			this.typeName.set(room.getTypeName());
		}
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
		if(customer != null) {
			this.name.set(customer.getName());
			this.phone.set(customer.getPhone());
		}
	}
	
}
